var mongoose = require('mongoose');
var validate = require('mongoose-validator');


var emailValidator = [
    validate({
            validator: 'isEmail',
            message: '邮箱格式不正确'
        })
];


var passValidator = [
    validate({
        validator: 'isLength',
        arguments: [6, 32],
        message: '密码长度需要在 {ARGS[0]} - {ARGS[1]} 之间'
        }),
    validate({
        validator: 'matches',
        arguments: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{6,12}$/,
        message: '密码必须包含大小写和特殊字符'
    })
];


var Schema = mongoose.Schema;
var Users = new Schema({
    email: { type: String, required: true, unique: true, validate: emailValidator  },
    password: {type: String, required: true,  validate: passValidator },
    createtime: { type: Date, required: true, default: Date.now  },
    updatetime: { type: Date, default: Date.now  }
});


module.exports = db.model('Users', Users);